package org.ftz.control;
import javax.sound.sampled.SourceDataLine;
import java.io.IOException;
import java.io.InputStream;
public class ReceiveAudio implements Runnable {  //接受音频
    public  byte[] bis = new byte[2024];
    public  InputStream inAudio;
    public  SourceDataLine sourceDataLine;
    public ReceiveAudio(byte[] bis, InputStream inAudio, SourceDataLine sourceDataLine) {
        this.bis = bis;
        this.inAudio = inAudio;
        this.sourceDataLine = sourceDataLine;
    }
    @Override
    public void run() {
        while (true) {
            int readLen = 0;
            try {
                readLen = inAudio.read(bis);
            } catch (IOException e) {
                try {
                    inAudio.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                e.printStackTrace();
            }
            if (bis != null) {
                System.out.println("Server收声音"); //播放对方发送来的音频
                sourceDataLine.write(bis, 0, readLen);
            }
        }
    }
}